//
//  SearchViewController.m
//  BabyAndBabyDriver
//
//  Created by 范琦 on 16/8/24.
//  Copyright © 2016年 范琦. All rights reserved.
//

#import "SearchViewController.h"
#import "SearchV.h"

@interface SearchViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UIView *mainView;
@property (nonatomic, strong) SearchV *serchView;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *arr;

@end

@implementation SearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"搜索";
    self.mainView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, KSCREENHEIGHT)];
    _mainView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_mainView];
    [self creatSearch];
    [self creatTableView];
    
}

- (void)creatSearch
{
    _serchView = [[[NSBundle mainBundle] loadNibNamed:@"SearchV" owner:nil options:nil] lastObject];
    _serchView.frame = CGRectMake(0, 10, KSCREENWIDTH , 40);
    [_mainView addSubview:_serchView];
    [[_serchView.searchBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        [self netHandle];

    }];
}
- (void)creatTableView
{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 50, KSCREENWIDTH, KSCREENHEIGHT - 50) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.rowHeight = 30;

    [self.view addSubview:_tableView];
    
    _tableView.showsVerticalScrollIndicator = NO;
    
    
}
- (void)netHandle
{
    self.arr = [NSMutableArray array];
    
        YTKNetHandle *api = [[YTKNetHandle alloc]initWithUrl:@"Order/searchMerchantVague.json" paramDic:@{@"token":TOKEN, @"name_merchant":_serchView.text.text} RegisterApiMethod:RequestMethodPost];
        [api startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
            
            NSArray *arr = request.responseBody[@"result"][@"data"];
            for (NSDictionary *dic in arr) {
                
                
                [_arr addObject:dic];
                
            }
            [_tableView reloadData];
        } failure:nil];
    
    




}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _arr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
    }
    NSDictionary *dic = _arr[indexPath.row];
    cell.textLabel.text = [NSString stringWithFormat:@"商户名称:%@  商户编号:%@",dic[@"name_merchant"], dic[@"user_name"]];
    cell.textLabel.font = [UIFont systemFontOfSize:12];
    cell.textLabel.textColor = TEXTCOLOR;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    NSDictionary *dic = _arr[indexPath.row];
    [self.rac_subject sendNext:dic[@"user_name"]];
    [self.navigationController popViewControllerAnimated:YES];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
